package DianShang_2024.ds_07.extract

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.{lit, to_timestamp}

import java.text.SimpleDateFormat
import java.util.{Calendar, Properties}

object extract04 {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取的第四题")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
//      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
//      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .enableHiveSupport()
      .getOrCreate()

      val connect=new Properties()
      connect.setProperty("user","root")
    connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_store?useSSL=false","base_region",connect)
  .createOrReplaceTempView("mysql_data")

    spark.table("ods07.base_region")
      .createOrReplaceTempView("ods")

    val day:Calendar=Calendar.getInstance()
    val current_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(day.getTime)
    day.add(Calendar.DATE,-1)
    val yesterday=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(day.getTime)

    spark.sql(
      """
        |select
        |m.*
        |from mysql_data as m
        |join ods as o
        |on o.id=m.id
        |where m.id not in(select id from ods)
        |""".stripMargin)
      .withColumn("create_time",to_timestamp(lit(current_time)))
      .withColumn("etl_date",lit(yesterday))
      .write.mode("append")
      .format("hive")
      .partitionBy("etl_date")
      .saveAsTable("ods07.base_region")




    spark.close()
  }

}
